<?php

namespace app\api\controller;

use app\api\model\OauthIntegral AS IntegralModel;
use app\api\model\OauthOrderClass;
use app\api\validate\Page;

class OauthIntegral extends Base
{
    /**
     * 积分变化列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        // type>0，则获取type类型的，type=0否则获取全部
        $type = input('type');
        $page = $data['page'] = input('page') ? input('page') : 1;
        $size = $data['size'] = input('size') ? input('size') : $this->pageSize;

        if ($type > 0) {
            $typeArr = IntegralModel::attributes('type');
            if (!isset($typeArr[$type])) {
                return self::sendResult(102, [], '类型错误');
            }
        }

        // 检测变量
        $validate = new Page();
        if (!$validate->check($data)) {
            return self::sendResult(102, [], $validate->getError());
        }

        $oauthId = $this->oauthInfo['id'];
        $integral = $this->oauthInfo['integral'];

        $model = IntegralModel::getList($oauthId, $integral, $type, $page, $size);

        // 判断当天是否已经签到
        $log = IntegralModel::getOne($oauthId, IntegralModel::TYPE_SIGN);
        $model['isSign']= $log ? 1 : 0;

        return self::sendResult(self::$okCode, $model);
    }

    /**
     * 签到
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function sign()
    {
        $oauthId = $this->oauthInfo['id'];

        // 签到类型
        $type = IntegralModel::TYPE_SIGN;

        // 判断当天是否已经签到
        $log = IntegralModel::getOne($oauthId, $type);
        if ($log) {
            return self::sendResult(114);
        }

        // 需要加上的签到积分
        $addNum = IntegralModel::getNum($oauthId, $type);

        // 添加签到记录
        $res = IntegralModel::addOne($oauthId, $type, $addNum);

        if ($res === true) {
            $code = self::$okCode;
            $msg = '签到成功';
        } else {
            $code = 115;
            $msg = '签到失败';
        }
        return self::sendResult($code, [], $msg);
    }

    /**
     * 完成积分任务加积分
     * @param $classId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addIntegral($classId)
    {
        // 积分获取类型，1、视频学习，2音频学习，3背单词，4背句子
        $type = input('type');

        if ($classId < 0) {
            return self::sendResult(100);
        }

        $typeArr = [
            IntegralModel::TYPE_VIDEO,
            IntegralModel::TYPE_AUDIO,
            IntegralModel::TYPE_WORD,
            IntegralModel::TYPE_SENTENCE
        ];

        if (!in_array($type, $typeArr)) {
            return self::sendResult(102, [], '类型错误');
        }

        $oauthId = $this->oauthInfo['id'];
        // 加积分必须是自己已经购买的班级，判断是否已经购买
        $one = OauthOrderClass::getOneByClass($oauthId, $classId);
        if (empty($one)) {
            return self::sendResult(109);
        }

        // 观看视频、音频10分钟加积分
        $addNum = IntegralModel::getNum($oauthId, $type);

        // 添加积分记录
        $res = IntegralModel::addOne($oauthId, $type, $addNum);

        if ($res === true) {
            $code = self::$okCode;
            $msg = '完成积分任务，加' . $addNum . '积分';
        } else {
            $code = 116;
            $msg = '加积分失败，超出每天限额';
        }
        return self::sendResult($code, [], $msg);
    }
}